package com.whut.userservice.controller;

import com.whut.clcommon.entity.Result;
import com.whut.clcommon.utils.AliOSSUtils;
import com.whut.userservice.domain.dto.RegisterDTO;
import com.whut.userservice.domain.dto.UpdateUserDTO;
import com.whut.userservice.domain.dto.UserLoginDTO;
import com.whut.userservice.domain.po.User;
import com.whut.userservice.domain.vo.UserVO;
import com.whut.userservice.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    IUserService userService;

    @Resource
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/login")
    public Result<String> login(@RequestBody @Validated UserLoginDTO loginFormDTO){
        String token = userService.login(loginFormDTO);
        return Result.success(token);
    }

    @PostMapping("/register")
    public Result<?> register(@RequestBody @Validated RegisterDTO registerDTO){
        userService.register(registerDTO);
        return Result.success();
    }

    @PostMapping("/modify")
    public Result<UserVO> modifyUser(@RequestBody @Validated UpdateUserDTO updateUserDTO){
        UserVO vo = userService.modify(updateUserDTO);
        return Result.success(vo);
    }

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile image) throws IOException {
        String url = aliOSSUtils.upload(image);
        return Result.success(url);
    }

    @GetMapping("/getUserInfo")
    public Result<UserVO> getUserInfo(@RequestParam @NotNull Long userId){
        User user = userService.getById(userId);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        userVO.setGender(user.getGender().getValue());
        return Result.success(userVO);
    }


}
